#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include <stdlib.h>
#include <string.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "gestion_stock.h"
#include <stdio.h> 
#include <string.h>
#include"temps.h"
#include<time.h>
int validation_ajout=0; //variable ajout du produit en cas d'existance 

 GtkWidget *window5;

void
on_button23_clicked                    (GtkWidget *objet_graphique, gpointer user_data)
{int count=0;
count++;
if(count==1)
window5 = create_window5 ();
gtk_widget_show (window5);

}

//****************************************************************************************************
void
on_button24_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//********************************************************************************************************
void
on_button25_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************
void
on_button26_clicked                    (GtkWidget *objet_graphique, gpointer user_data)
{
int count=0;GtkWidget *window6;
count++;
if(count==1)
window6 = create_window6 ();
gtk_widget_show (window6);
}

//****************************************************************************************************
void
on_button8_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************
void
on_button9_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************
void
on_button22_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************
void
on_button12_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}
//****************************************************************************************************
GtkWidget *window4;
int count=0;
void on_button7_clicked (GtkWidget *objet_graphique, gpointer user_data)
{
count++;
if (count==1)
{window4 = create_window4 ();
  gtk_widget_show (window4);}



}

//****************************************************************************************************
void
on_button27_clicked                    (GtkWidget *objet_graphique, gpointer user_data) //AHMED
{
 	GtkWidget *entry7;
	GtkWidget *entry8;
	GtkWidget *entry9;
	produit p,p1;
	int h=10001;
//p=malloc(sizeof(produit));
 //p1=malloc(sizeof(produit));
	char *c; c=malloc(sizeof(char)*20);
	GtkWidget * label97;
	label97= lookup_widget(objet_graphique, "label97");
	entry7 = lookup_widget(objet_graphique, "entry7");
	entry8 = lookup_widget(objet_graphique, "entry8");
	entry9 = lookup_widget(objet_graphique, "entry9");
	strcpy(p.nom, gtk_entry_get_text(GTK_ENTRY(entry7)));
	p.id=atoi(gtk_entry_get_text(GTK_ENTRY(entry8)));
	p.cont=atoi(gtk_entry_get_text(GTK_ENTRY(entry9)));
	p.last=h;
   	c=get_temps(p.t);
   	strcpy(p.t,c);
	
	
	
	if(existe_produit(p.id,p.nom)==0)
	    {   ajout_produit(p,h);
		char Buf0[500] = "Le produit est ajouté ";
		sprintf(Buf0,"%s",Buf0);
		gtk_label_set_text(GTK_LABEL(label97), Buf0);
  	    }
        else if (existe_produit(p.id,p.nom)==1) 
	     {  char Buf0[500] = "Le produit existe déja voulez vous ajouter cette quantité à ce produit ";
		sprintf(Buf0,"%s",Buf0);
		gtk_label_set_text(GTK_LABEL(label97), Buf0);
				if(validation_ajout==1) 
                			ajout_produit(p,h);
				
                
             }
        else {
		char Buf0[500] = "Le produit existe avec un nom ou un id différent ";
		sprintf(Buf0,"%s",Buf0);
		gtk_label_set_text(GTK_LABEL(label97), Buf0);
	      }

}
//****************************************************************************************************
int n=0;
void on_button28_clicked (GtkWidget *objet_graphique, gpointer user_data) //AHMED
{     n++;
	GtkWidget * label37;
	GtkWidget * label50;
	int a;
	//p=malloc(sizeof(produit));
		label37= lookup_widget(objet_graphique, "label37");
		label50= lookup_widget(objet_graphique, "label50");
		a=nbr_produit();
	        a=a/2+1;
	  //p=NULL;
	  if (n<a){      
		produit p;
	     	char chemin[200]="stock.txt";
		char Buf0[500] = "NOM: ";
		char Buf1[50] = "ID: ";
		char Buf2[50] = "quantité: ";
		char Buf3[50] = "Last    ";
		char Buf4[50]= " à ";

		p=afficher_stock(n);
		sprintf(Buf0, "%s %s \n\n %s%d \n\n %s%d \n\n  ", Buf0, p.nom, Buf1,p.id,Buf3,a);//p.last);
		sprintf(Buf2, "%s%d \n\n%s %s",Buf2, p.cont,Buf4, p.t);

		gtk_label_set_text(GTK_LABEL(label37), Buf0);
		gtk_label_set_text(GTK_LABEL(label50), Buf2);}


           else {char Buf5[500] = "Fin du fichiers produit";
		char Buf6[500] =" ";		
		sprintf(Buf5, "%s",Buf5);
		sprintf(Buf6, "%s",Buf6);
		gtk_label_set_text(GTK_LABEL(label37), Buf5);
		gtk_label_set_text(GTK_LABEL(label50), Buf6);}
	//free(p);//free(x);

}

//****************************************************************************************************

void on_window6_destroy (GtkObject *object, gpointer user_data)
{
	gtk_main_quit();
}
//****************************************************************************************************

void on_window4_destroy (GtkObject *object, gpointer user_data)
{
	count=0;
}
//****************************************************************************************************

void
on_button29_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button30_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


//****************************************************************************************************
void
on_button13_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button18_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************
void
on_button15_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button16_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button17_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button31_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button34_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button33_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button32_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button35_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button37_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button38_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}
//****************************************************************************************************


void
on_button39_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}
//****************************************************************************************************


void
on_button40_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button43_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button42_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button41_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button44_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

//****************************************************************************************************

void
on_button45_clicked                    (GtkWidget *objet_graphique, gpointer user_data)
{

}
//****************************************************************************************************
int id_personnel=10002;
int signe=1;
void on_button46_clicked  (GtkWidget *objet_graphique, gpointer user_data)      //AHMED
{	int id,cont,test;       
	GtkWidget *entry20;
	GtkWidget *entry21;
        GtkWidget * label96;
	label96= lookup_widget(objet_graphique, "label96");
	entry20 = lookup_widget(objet_graphique, "entry20");
	entry21 = lookup_widget(objet_graphique, "entry21");
	id=atoi(gtk_entry_get_text(GTK_ENTRY(entry20)));
	cont=atoi(gtk_entry_get_text(GTK_ENTRY(entry21)));
        



	if(signe==1)
   	{     test=modifier_produit(id,cont,id_personnel);
				if (test==1) { 
					char Buf0[500] = "Le produit est modifié ";
					sprintf(Buf0,"%s",Buf0);
					gtk_label_set_text(GTK_LABEL(label96), Buf0);
					      }
				else  	      { 
					char Buf0[500] = "Le produit n'existe pas ";
					sprintf(Buf0,"%s",Buf0);
                                        gtk_label_set_text(GTK_LABEL(label96), Buf0);
					       }					
	}
     	else 
	{cont=-cont;     test=modifier_produit(id,cont,id_personnel);

                                if (test==1) { 
					char Buf0[500] = "Le produit est modifié ";
					sprintf(Buf0,"%s",Buf0);
					gtk_label_set_text(GTK_LABEL(label96), Buf0);
					      }
				else  	      { 
					char Buf0[500] = "Le produit n'existe pas ";
					sprintf(Buf0,"%s",Buf0);
					gtk_label_set_text(GTK_LABEL(label96), Buf0);
					      }	    	
        }
}

//****************************************************************************************************

void
on_button47_clicked                    (GtkWidget *objet_graphique, gpointer user_data)
{GtkWidget *window15;
window15 = create_window15 ();
  gtk_widget_show (window15);
}

//****************************************************************************************************

void
on_radiobutton1_pressed                (GtkButton       *button,   
                                        gpointer         user_data)           //AHMED
{
signe=1;
}

//****************************************************************************************************

void
on_button48_clicked                    (GtkWidget *objet_graphique, gpointer user_data)
{

}

//****************************************************************************************************

void
on_checkbutton1_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)		//AHMED
{
validation_ajout=1;
}


void
on_radiobutton2_pressed                (GtkButton       *button,
                                        gpointer         user_data)
{
signe=-1;
}

